package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.bean.CodeMsg;
import com.sise.entity.ProductLike;
import com.sise.mapper.ProductLikeMapper;
import com.sise.service.IProductLikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zj
 */
@Service
public class ProductLikeServiceImpl extends ServiceImpl<ProductLikeMapper, ProductLike> implements IProductLikeService {

    @Resource
    private ProductLikeMapper productLikeMapper;

    @Override
    public boolean addLike(ProductLike productLike) {
        int tag = productLikeMapper.insert(productLike);
        return tag == 1;
    }

    @Override
    public boolean cancelLike(ProductLike productLike) {
        QueryWrapper<ProductLike> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("product_id", productLike.getProductId())
                .eq("open_id", productLike.getOpenId());
        int tag = productLikeMapper.delete(queryWrapper);
        return tag == 1;
    }

    @Override
    public CodeMsg isLikeAndCount(ProductLike productLike) {
        QueryWrapper<ProductLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productLike.getProductId())
                .eq("open_id", productLike.getOpenId());
        ProductLike getProductLike = productLikeMapper.selectOne(queryWrapper);
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productLike.getProductId());
        int count = productLikeMapper.selectCount(queryWrapper);
        if (getProductLike != null) {
            //setMsg为1代表该用户喜欢，0代表用户没有喜欢，setData存放的是有多少个用户喜欢该商品
            return new CodeMsg().setCode(200).setMsg("1").setData(count);
        } else {
            return new CodeMsg().setCode(400).setMsg("0").setData(count);
        }
    }
}
